;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.InventoryWarning", [])
        .controller("InventoryWarningCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };

            $scope.condition = {};
            $scope.dataList = [];

            $scope.exportParam = {};

            if ($scope.isKJ) {
                $scope.exportParam.allUrl = ybHttp.RequestUrl("dReport", "/newreport/exprotSafeWarning");

                $scope.tbConf = {
                    scrollWidth: 900,
                    tableCol: [
                        {
                            name: "零件编号",
                            dataField: "materialsNo",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "零件中文名",
                            dataField: "materialsDesc",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "安全库存",
                            dataField: "safeQty",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "实际库存",
                            dataField: "warehouseQty",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "采购订单数量",
                            dataField: "purchaseQty",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "占用数量",
                            dataField: "occupyQty",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "建议采购数量",
                            dataField: "adviceQty",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                    ]
                };
            } else {
                $scope.exportParam.allUrl = ybHttp.RequestUrl("device", "/report/exportinventoryWarning");

                $scope.tbConf = {
                    scrollWidth: 1600,
                    tableCol: [
                        {
                            name: "零件编号",
                            dataField: "materialsNo",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "零件中文名",
                            dataField: "materialsDes",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "需求数量",
                            dataField: "needQty",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "总库存",
                            dataField: "totalQty",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "计划外仓库库存",
                            dataField: "outInventory",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "计划内仓库库存",
                            dataField: "inInventory",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "缺货数量",
                            dataField: "lackNum",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "月均需求量",
                            dataField: "averageNum",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "出库次数",
                            dataField: "orderNum",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
    
                        {
                            name: "零件系数",
                            dataField: "ratio",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "安全库存下限",
                            dataField: "safeAmount",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "安全库存上限",
                            dataField: "maxAmount",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "采购计划下限",
                            dataField: "purchaseMinNum",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "采购计划上限",
                            dataField: "purchaseMaxNum",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        }
                    ]
                };
            }

            $scope.reset = function () {
                $scope.condition = {};
            };

            var btnPermissionCode = {
                export: window.ActionCode.inventoryWarningExport
            };

            $scope.btnPermission = {
                export: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                var service = "device";
                var reqUrl =  "/report/inventoryWarningList";
                if ($scope.isKJ) {
                    service = 'dReport';
                    reqUrl = '/newreport/safeWarning';
                }
                
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.inDateStr = $filter("YbDate")(item.inDate, "yyyy-MM-dd HH:mm:ss");
                        item.createDateStr = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            (function () {
                //getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);